<template>
    <div class="container block">
        <div class="header">
            <div class="search">
                <input
                    type="text"
                    placeholder="请输入人员/派遣单位/项目名称"
                    class="input search-input"
                    v-model="searchText"
                    @keydown.enter="search"
                />
                <button class="btn" @click="search">搜索</button>
                <button class="btn-white" @click="onReset">重置</button>
            </div>
            <!-- <div><button class="btn" @click="isEnterprise = true">添加企业</button></div> -->
        </div>
        <div class="table">
            <el-table
                :data="tableData"
                style="width: 100%"
                :header-row-style="{ color: '#202536' }"
                :row-style="{ color: '#5E617D' }"
            >
                <el-table-column
                    width="300"
                    prop="serveCompanyName"
                    label="派遣单位名"
                />
                <el-table-column prop="ppCount" label="项目数" />
                <el-table-column prop="progressCount" label="进行中的项目" />
                <el-table-column prop="contacts" label="负责人" />
                <!-- <el-table-column prop="pwCount" label="总人数" /> -->
                <el-table-column label="操作">
                    <template #default="scope">
                        <p class="link" @click="goEnterprise(scope.row.id)">
                            查看详情
                        </p>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                class="pagination"
                v-model:currentPage="pageNum"
                v-model:page-size="pageSize"
                :page-sizes="[10, 30, 50, 100]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="getList"
            >
            </el-pagination>
        </div>
        <popupTeamMember @close="isPopupTeamMember = false" selectType="one" @determine="onAddHead"  v-if="isPopupTeamMember" dataTitle="添加成员"></popupTeamMember>
        <popupEnterprise @close="isEnterprise = false" @determine="onAddEnterprise"  @selectHead="onSelectHead" :headList="headList"  v-if="isEnterprise" dataTitle="添加企业"></popupEnterprise>
    </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { getEnterpriseList, EnterpriseDto } from '@/api/enterprise'
import { addBindCompanySupplier } from '@/api/home'
import popupTeamMember from '@/components/popup-team-member.vue'
import popupEnterprise from '@/components/popup-enterprise.vue'
export default defineComponent({
    name: 'Enterprise',
    components: {
        popupTeamMember,
        popupEnterprise
    },
    data () {
        return {
            isEnterprise: false,
            isPopupTeamMember: false,
            tableData: [] as EnterpriseDto[],
            pageSize: 10,
            pageNum: 1,
            total: 0,
            searchText: '',
            searchValue: '',
            headList: []
        }
    },
    created () {
        this.getList()
    },
    methods: {
        async getList () {
            const result = await getEnterpriseList({
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                searchText: this.searchValue
            })
            this.total = result.total
            this.tableData = result.list
        },
        // 选择负责人回调
        onAddHead (event:any) {
            console.log(event, 'event')
            this.headList = event
            console.log(this.headList, 'headList')
            this.isPopupTeamMember = false
        },
        handleSizeChange () {
            this.pageNum = 1
            this.getList()
        },
        goEnterprise (id: number) {
            this.$router.push({
                path: '/enterprise/detail/' + id
            })
        },
        search () {
            this.searchValue = this.searchText
            this.pageNum = 1
            this.getList()
        },
        onReset () {
            this.searchText = ''
            this.search()
        },
        // 打开选择负责人弹窗
        onSelectHead () {
            this.isPopupTeamMember = true
        },
        // 确认添加企业回调
        async onAddEnterprise (head:any, enterprise:any, phone:string, switchCard:boolean, isAdvance: any, isPhone: any) {
            console.log(head, enterprise, phone, switchCard)
            addBindCompanySupplier({
                area: enterprise.area || '暂无', // 公司位置
                contacts: head[0].userName, // 选择的name
                contactsId: head[0].id, // 选择的id
                createUserId: 2, // user.userId
                creditCode: enterprise.creditCode,
                isNeedIdcardPhoto: switchCard ? 1 : 0, // 是否需要照片
                phone: phone, // 电话
                serveCompanyName: enterprise.name, // 公司名称
                isOpenAdvance: isAdvance * 1,
                isShowContacts: isPhone * 1
            }).then(() => {
                this.isEnterprise = false
                this.$message.success('创建成功')
            }).catch(() => {
                this.isEnterprise = true
            })
        }
    }
})
</script>

<style lang="stylus" scoped>
table()
</style>
